package com.jyg.common.result;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;

/**
 * @author 橘白
 */
@Data
@AllArgsConstructor
public class ResultPage<D> implements Serializable {
    /**
     * 查询数据列表
     */
    private Collection<D> dataList;
    /**
     * 总数
     */
    private long total;
    /**
     * 每页显示条数，默认 10
     */
    private long pageSize;
    /**
     * 当前页
     */
    private long pageNum = 1;

    public ResultPage(Collection<D> dataList) {
        if (null == dataList) {
            this.dataList = Collections.emptyList();
            this.total = 0;
            this.pageSize = 0;
        } else {
            this.dataList = dataList;
            this.total = dataList.size();
            this.pageSize = dataList.size();
        }
    }

    public ResultPage(IPage<D> page) {
        if (null == page) {
            this.dataList = Collections.emptyList();
            this.total = 0;
            this.pageSize = 0;
        } else {
            this.dataList = page.getRecords();
            this.total = page.getTotal();
            this.pageSize = page.getSize();
            this.pageNum = page.getCurrent();
        }
    }
}